#include "zf_common_headfile.h"
// #include "midline_process.h"

unsigned char   track_Width[POINTS_MAX_LEN];//赛道宽度
#define LINE_GAP 160 //两线差距

//内倾连续值
unsigned char l_trend[POINTS_MAX_LEN];//左内倾连续数 1外倾 2内倾  0没有有用信息 内倾判断"理论"上是在不丢线的情况下进行的,但实际中右边界可能是 181 181 181 181所以倾斜判断不应该是圣经,只能是辅助,还是非常有用的
unsigned char r_trend[POINTS_MAX_LEN];//右内倾连续数 1内倾(对于右边线就是左倾) 2外倾(右倾) 0没有有用信息


void ExtractCenterline(void)
{
    unsigned char mi;

memset(l_trend,0,POINTS_MAX_LEN);
memset(r_trend,0,POINTS_MAX_LEN);



//中线合成
// rpts_num=min(iptsr_num,iptsL_num)-LSC_STEP;
rpts_num = min(iptsr_num,iptsL_num);
unsigned char  midy;
for (midy = ROW_Start; midy >=ROW_Start-rpts_num ; midy--)
{
    rpts[midy]=(iptsr[midy]+iptsL[midy])/2;
}
// #define WIDTH_P_SRAND
// #define WIDTH_D_SRAND
if(rpts_num>=20)
{
    //记录左右内外倾
    for(mi=ROW_Start;mi>ROW_Start-rpts_num+2;mi--) //末尾5行不计算
    {
        if(mi>=ROW_Start-5) //末尾5行,向上判断
        {
            if(iptsL[mi]<=iptsL[mi-1] && iptsL[mi]<=iptsL[mi-2] && iptsL[mi]>=COL_Start && iptsL[mi-2]>=COL_Start) //右倾趋势 且不丢线
            {
                l_trend[mi]=2;//右倾
                // printf("l_trend:%d \n",l_trend[mi]);
            }else if(iptsL[mi]>iptsL[mi-1] && iptsL[mi]>iptsL[mi-2] && iptsL[mi]>=COL_Start && iptsL[mi-2]>=COL_Start) //去掉=号,左边线的左倾应该严格
            {
                l_trend[mi]=1; //左倾
                // printf("mi: %d l_trend:%d \n",mi,l_trend[mi]);
            }

            if(iptsr[mi]>=iptsr[mi-1] && iptsr[mi]>=iptsr[mi-2] && iptsr[mi]<COL_End && iptsr[mi-2]<COL_End){
                r_trend[mi]=1; //左倾
                // printf("r_trend:%d \n",r_trend[mi]);
            }else if(iptsr[mi]<iptsr[mi-1] && iptsr[mi]<iptsr[mi-2] && iptsr[mi]<COL_End && iptsr[mi-2]<COL_End) //右倾趋势 且不丢线 去掉＝号,右边线的右倾应该严格
            {
                r_trend[mi]=2;//右倾
                // printf("r_trend:%d \n",r_trend[mi]);
            }
        }
        else //其余行数,向下判断
        {
            if(iptsL[mi]>=iptsL[mi+1] && iptsL[mi]>=iptsL[mi+2] && iptsL[mi]>=COL_Start && iptsL[mi+2]>=COL_Start) //右倾趋势 且不丢线
            {
                l_trend[mi]=2;//右倾
                // printf("l_trend:%d \n",l_trend[mi]);
            }else if(iptsL[mi]<iptsL[mi+1] && iptsL[mi]<iptsL[mi+2] && iptsL[mi]>=COL_Start && iptsL[mi+2]>=COL_Start) //去掉=号,左边线的左倾应该严格
            {
                l_trend[mi]=1; //左倾
                // printf("l_trend:%d \n",l_trend[mi]);
            }

            if(iptsr[mi]>iptsr[mi+1] && iptsr[mi]>iptsr[mi+2] && iptsr[mi]<COL_End && iptsr[mi+2]<COL_End) //右倾趋势 且不丢线 去掉＝号,右边线的右倾应该严格
            {
                r_trend[mi]=2;//右倾
                // printf("r_trend:%d \n",r_trend[mi]);
            }else if(iptsr[mi]<=iptsr[mi+1] && iptsr[mi]<=iptsr[mi+2] && iptsr[mi]<COL_End && iptsr[mi+2]<COL_End){
                r_trend[mi]=1; //左倾
                // printf("r_trend:%d \n",r_trend[mi]);
            }
        }


    }
}

//中线处理
// if(rpts_num>=40)
// {

//     for ( mi = ROW_Start-20; mi >=ROW_Start-rpts_num; mi--) //末尾20行不计算
//     {

//         if(iptsL[mi]<iptsL[mi+1] && iptsL[mi]<iptsL[mi+2] && RowLose[mi]==2 && RowLose[mi+1]==2 /* && iptsL[mi+2]-iptsL[mi] >=5 */) //左边线减小趋势 右边线存在丢线 //右斜入十字就在其中
//         {

//             rpts[mi] = rpts[mi+1]+1;
//             rpts[mi]=(rpts[mi]>COL_End?COL_End:rpts[mi]);
//             rpts[mi]=(rpts[mi]<COL_Start?COL_Start:rpts[mi]);
//         }

//             if(iptsr[mi]>iptsr[mi+1] && iptsr[mi]>iptsr[mi+2] && RowLose[mi]==3 && RowLose[mi+1]==3) //右边线增加趋势 左边线都丢
//         {

//             rpts[mi] = rpts[mi+1]-1;
//             rpts[mi]=(rpts[mi]>COL_End?COL_End:rpts[mi]);
//             rpts[mi]=(rpts[mi]<COL_Start?COL_Start:rpts[mi]);
//         }

//         if(RowLose[mi]==4) //双边丢线
//         {
//             rpts[mi] = rpts[mi+1];
//             rpts[mi]=(rpts[mi]>COL_End?COL_End:rpts[mi]);
//             rpts[mi]=(rpts[mi]<COL_Start?COL_Start:rpts[mi]);
//         }

//     }
// }
}

